"""
闭包
在函数嵌套的前提下，内部函数使用了外部函数的变量，并且外部函数返回了内部函数，我们吧这个使用外部函数变量的内部函数称为闭包
"""

# 简单闭包
# def outer(logo):
#     def inner(msg):
#         print(f"<{logo}>{msg}<{logo}>")
#     return inner
#
# fn1 = outer("黑马程序员")
# fn1("大家好")
# fn1("大家好")

# 使用 nonlocal 关键字修改外部函数的值
# def outer(num1):
#     def inner(num2):
#         nonlocal num1
#         num1 += num2
#         print(num1)
#     return inner
#
# fn = outer(10)
# fn(10)
# fn(10)

def account_create(initial_amount = 0):
    def atm(num,deposit=True):
        nonlocal initial_amount
        if deposit:
            initial_amount += num
            print(f"存款：+{num}，账户余额：{initial_amount}")
        else:
            initial_amount -= num
            print(f"取款：-{num}，账户余额：{initial_amount}")

    return atm


atm = account_create(100)

atm(100,True)
atm(100,True)
atm(50,False)

account_create(50)

atm(10)
